package com.huida.app.hutoolsusage;

import cn.hutool.core.util.ReUtil;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author wangzhuangzhuang
 * @DESC: 正则工具类
 * @date 2022-01-11 09:47
 */

@SpringBootTest
public class ReUtilUsageDemo {

	@Test
	public void test01(){
//		String content = "ZZZaaabbbccc中文1234";
//		Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
//		Matcher matcher = pattern.matcher(content);
//		if (matcher.find()) {
//			String result= matcher.group();
//		}
	}

	// ReUtil.extractMulti
	// 抽取多个分组然后将其拼接起来
	@Test
	public void test02(){
		String content = "ZZZaaabbccc中文1234";
		String resultExtractMulti =  ReUtil.extractMulti("(\\w)aa(\\w)", content, "$1-$2");
		Assert.assertEquals("Z-a", resultExtractMulti);
	}

	// ReUtil.isMatch
	// 给定字符串是否匹配给定正则
	@Test
	public void test03(){
		String content = "ZZZaaabbbcc中文1234";
		boolean isMatch = ReUtil.isMatch("\\w+[\u4E00-\u9FFF]+\\d+", content);
		Assert.assertTrue(isMatch);
	}

	// ReUtil.replaceAll
	// 通过正则查找到字符串，然后匹配到字符串加入到replacementTemplate中, $1表示分组1的字符串
	@Test
	public void test04(){
		String content = "ZZZaaabbbccc中文1234";
		// 把1234替换为 ->1234<-
		String replaceAll = ReUtil.replaceAll(content, "(\\d+)", "->$1<-");
		Assert.assertEquals("ZZZaaabbbccc中文->1234<-", replaceAll);
	}

	// ReUtil.escape
	// 转义给定字符串，为正则相关的特殊符号转义
	@Test
	public void test05(){
		String escape = ReUtil.escape("我有个$符号{}");
		System.out.println("str: " + escape); // str: 我有个\$符号\{\}
	}
}